from typing import List, Dict, Tuple
from BPRMF.FileLoader import Interaction
import torch


class InteractScore:
    def __init__(self, hyper_params):
        self.hyper_params = hyper_params

    def get_interact_score(self, user_interact_list: List[Interaction]) -> Tuple[Dict[int, float], Dict[int, torch.Tensor]]:
        interact_dict: Dict[int, Dict[int, List[Interaction]]] = {}

        # Group By interaction_type and book ID
        for interact in user_interact_list:
            if interact.interaction_type not in interact_dict:
                interact_dict[interact.interaction_type] = {}

            if interact.item_id not in interact_dict[interact.interaction_type]:
                interact_dict[interact.interaction_type][interact.item_id] = []

            interact_dict[interact.interaction_type][interact.item_id].append(
                interact)

        # Process each interaction_type respectively
        result: Dict[int, float] = {}

        for interact_type, book_hist in interact_dict.items():
            if interact_type == 0:
                # enter the reading page
                for book_id, hist in book_hist.items():
                    result[book_id] = float(len(hist))

        return result
